package cz.data.domain.market.model.handlers;

import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import cz.data.domain.market.model.dto.ServiceReqParam;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.util.List;

@MappedTypes({Object.class})
@MappedJdbcTypes({JdbcType.VARCHAR})
public class ServiceReqParamArrayTypeHandler extends JacksonTypeHandler {
    private final TypeReference<List<ServiceReqParam>> reference = new TypeReference<List<ServiceReqParam>>() {
    };

    public ServiceReqParamArrayTypeHandler(Class<?> type) {
        super(type);
    }

    @Override
    protected Object parse(String json) {
        try {
            return getObjectMapper().readValue(json, reference);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
